package job.manager.db.dao;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.ec.TimeReportEB;

/**
 *  DAO class is responsible for communcation with the
 *  database regarding timereports.
 */

public class TimeReportDAO {
    EntityManager em;
    

    public TimeReportDAO(EntityManager em)
    {
        this.em = em;
    }
    
    public TimeReportEB getTimeReport(Integer pk)
    {
        return em.find(TimeReportEB.class, pk);
    }

    public int update(TimeReportEB report){
        if(report != null){
            TimeReportEB r = em.merge(report);
            em.flush();
            if( em.contains(report))
                return 1;
        }
        return 0;
    }

    public TimeReportEB insert(TimeReportEB report){
        if(report != null){
            em.persist(report);
            em.flush();
            if( em.contains(report))
                return report;
        }
        return null;
    }

    public int delete(TimeReportEB report){
        if( report != null) {
            TimeReportEB r = em.merge(report);

            em.remove(r);

            if( !em.contains(r))
                return 1;
        }
        return 0;
    }

    public Collection<TimeReportEB> getAllTimeReport()
    {

        Query q = em.createNamedQuery("TimeReportEB.findAll");

        return q.getResultList();
    }
   
    public Collection<TimeReportEB> getAllTimeReportWorker(ProfilesEB pid)
    {
       Query q = em.createNamedQuery("TimeReportEB.findByPid");
       q.setParameter("profileId", pid);
       return q.getResultList();
    }

    public Collection<TimeReportEB> getAllTimeReportWorkerSortedByDate(ProfilesEB pid)
    {
       Query q = em.createNamedQuery("TimeReportEB.findByPidDateSorted");
       q.setParameter("profileId", pid);
       return q.getResultList();
    }

    public Collection<TimeReportEB> getAllTimeReportJob(Integer jid)
    {
       Query q = em.createNamedQuery("TimeReportEB.findByJid");
       q.setParameter("jobId", jid);
       return q.getResultList();
    }

}
